<?php
/**
 * IndexController.php
 *
 * 应用程序默认控制器
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Home
 * @package		Home_Controller
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Controller_Public */
//require_once 'Momoger/Controller/Public.php';

/**
 * 默认控制器
 *
 * @category	Home
 * @package		Home_Controller
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class IndexController extends Momoger_Controller_Public {

	/**
	 * 默认显示网站首页
	 *
	 * @return HTML
	 */
	public function indexAction() {
		//$this->logger()->debug('default home page');
		//
		//Zend_Debug::dump($this->user);

		//$user = new Momoger_Model_User();
		//Zend_Debug::dump($user);
		//$user->findRow('userID = 1');
		//Zend_Debug::dump($user->getAll());

	}

	/**
	 * 测试页面
	 *
	 * @return void
	 */
	public function testAction() {
		//auto redirect test
		//$this->redirectWithMsg(5,'now go to login page', NULL, '/login');

		//no use layout helper
		/**
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }
        //*/

        //no use view helper
		//$this->_helper->viewRenderer->setNoRender();
		//or
		$this->getHelper('ViewRenderer')->setNoRender();

		echo 'hello';
	}

	/**
	 * 系统配置信息
	 *
	 * @return HTML
	 */
	public function configmmmmAction() {
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }
		$this->_helper->viewRenderer->setNoRender();
		$key = $this->getRequest()->getParam('mfmumcmk');
		if ('i-am-a-pig-so-i-want-to-see-the-app-config' == $key) {
			echo '<pre>';
			$config = Zend_Registry::get(APP_CONFIG);
			$config['db']['params']['dbname'] = '***^o^***';
			$config['db']['params']['username'] = '***^o^***';
			$config['db']['params']['password'] = '***^o^***';
			$config['sess']['db']['params']['dbname'] = '***^o^***';
			$config['sess']['db']['params']['username'] = '***^o^***';
			$config['sess']['db']['params']['password'] = '***^o^***';
			$config['log']['writer']['db']['params']['dbname'] = '***^o^***';
			$config['log']['writer']['db']['params']['username'] = '***^o^***';
			$config['log']['writer']['db']['params']['password'] = '***^o^***';
			print_r($config);
			echo '</pre>';
		}
	}

	/**
	 * 通用页面头部信息组件
	 *
	 * @return HTML
	 */
	public function headerAction() {
		//set visitor information
		$this->view->user = $this->user;

		//set current view navigate
		$this->view->nav = $this->getRequest()->getParam('nav');
	}

	/**
	 * 通用页面底部信息组件
	 *
	 * @return HTML
	 */
	public function footerAction() {
		//todo
	}


	/**
	 * 用户登录页面
	 *
	 * @return HTML
	 */
	public function loginAction() {
		$cacheKey = 'homeIndexLoginForm';
		$formHtml = $this->cache()->get($cacheKey);
		if (!$formHtml) {
			$form = new Momoger_Form_Login();
			$formHtml = $form->getLoginForm($this->getHelper('Url')->direct('logined'));
			$this->cache()->set($cacheKey, $formHtml);
			//$this->logger()->debug('update cache by key: ' . $cacheKey);
		}
		$this->view->loginForm = $formHtml;
	}

	/**
	 * 处理用户登录提交数据
	 *
	 * @return HTML
	 */
	public function loginedAction() {
		//page view information time
		$delay = 3;

		$form = new Momoger_Form_Login();
		$loginForm = $form->getLoginForm();

		//valid user post data
		if (!$loginForm->isValid($_POST)) {
			$errors = $form->getErrorMsgs($loginForm->getErrors());
			$this->redirectWithMsg($delay, array_shift($errors), null, '/login');
			return false;
		}

		//validate user login
		$user = new Momoger_Model_User();
		if (!$user->userLogin($loginForm->getValues())) {
			$msgs = $user->getErrorMsg();
			$this->redirectWithMsg($delay, $msgs['msg'], null, $msgs['back']);
			return FALSE;
		}

		//page to home page
		$this->redirectWithMsg($delay, 'Thank you login system! Good luck!', null, '/');
		return TRUE;
	}

	/**
	 * 用户登出系统
	 *
	 * @return HTML
	 */
	public function logoutAction() {
		$user = new Momoger_Model_User();
		$user->userLogout();

		//page to home page
		$this->redirectWithMsg(3, 'You have logout safely!', null, '/');
		return TRUE;
	}

	/**
	 * 用户注册页面
	 *
	 * @return HTML
	 */
	public function registAction() {
		$cacheKey = 'homeIndexRegistForm';
		$formHtml = $this->cache()->get($cacheKey);
		if (!$formHtml) {
			$form = new Momoger_Form_Regist();
			$formHtml = $form->getRegistForm($this->getHelper('Url')->direct('registed'));
			$this->cache()->set($cacheKey, $formHtml);
			//$this->logger()->debug('update cache by key: ' . $cacheKey);
		}
		$this->view->registForm = $formHtml;
	}

	/**
	 * 处理用户注册提交数据
	 *
	 * @return HTML
	 */
	public function registedAction() {
		//page view information time
		$delay = 3;

		//get the form
		$form = new Momoger_Form_Regist();
		$registForm = $form->getRegistForm();

		//valid user post data
		if (!$registForm->isValid($_POST)) {
			$errors = $form->getErrorMsgs($registForm->getErrors());
			$this->redirectWithMsg($delay, array_shift($errors), null, '/regist');
			return false;
		}

		//get user post data
		$inputs = $registForm->getValues();

		//check user account been used or not
		$user = new Momoger_Model_User();
		if ($user->accountIsExists($inputs['account'])) {
			$msg = 'I am sorry to tell you. The account: ' . $inputs['account'] . ' has been used!';
			$this->redirectWithMsg($delay, $msg, null, '/regist');
			return false;
		}

		//save user data
		$user->saveUserRegist($inputs);

		//go back
		$msg = 'Hi <strong>' . $inputs['account'] . '</strong>! Welcome join us! Good luck!';
		$this->redirectWithMsg($delay, $msg, null, '/');
		return TRUE;
	}
}